package com.jgp.workflow.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.jgp.common.utils.JGPUtil;
import com.jgp.workflow.utils.CustomProcessDiagramGenerator;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.impl.jobexecutor.JobExecutor;
import org.activiti.spring.SpringJobExecutor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.Resource;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2018/11/14
 */
@Configuration
@ImportAutoConfiguration(exclude = SecurityAutoConfiguration.class)
public class WorkflowAutoConfiguration {
    
    @Resource
    private DruidDataSource dataSource;
    
    @Primary
    @Bean
    public TaskExecutor primaryTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // add necessary properties to the executor
        return executor;
    }
    
    @Bean
    public JobExecutor jobExecutor(TaskExecutor taskExecutor){
        SpringJobExecutor jobExecutor = new SpringJobExecutor(taskExecutor);
        return jobExecutor;
        
    }
    
    @Bean
    public ProcessEngineConfiguration processEngineConfigurationImpl(SpringProcessEngineConfiguration springProcessEngineConfiguration,JobExecutor jobExecutor){
        //设置ProcessEngineConfigurationImpl里的uuidGenerator
        springProcessEngineConfiguration.setDataSource(dataSource);
        springProcessEngineConfiguration.setIdGenerator(JGPUtil::uuid);
        //设置DbSqlSessionFactory的uuidGenerator，否则流程id，任务id，实例id依然是用DbIdGenerator生成
        springProcessEngineConfiguration.getDbSqlSessionFactory().setIdGenerator(JGPUtil::uuid);
        springProcessEngineConfiguration.setProcessDiagramGenerator(new CustomProcessDiagramGenerator());
        springProcessEngineConfiguration.setDatabaseSchemaUpdate("true");
        //springProcessEngineConfiguration.setJobExecutorActivate(true);
        /*List<SessionFactory> sessionFactories = new ArrayList<>();
        sessionFactories.add(customUserEntityManagerFactory);
        sessionFactories.add(customGroupEntityManagerFactory);
        springProcessEngineConfiguration.setCustomSessionFactories(sessionFactories);*/
        return springProcessEngineConfiguration;
    }
  
    /*@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        List<String> from = new ArrayList();
        from.add("/workflow/designer/**");
        List<String> to = new ArrayList();
        to.add("classpath:/back/workflow/designer");
        registry.addResourceHandler(from.toArray(new String[from.size()])).addResourceLocations(to.toArray(new String[to.size()]));
    }*/
    
}
